import { PIC_URL } from "./../../../../providers/Config";
import { BasePage } from "./../../../basePage";
import { StyleService } from "./../../../../providers/StyleService";
import { Component, Injector } from "@angular/core";
import { IonicPage, NavController, NavParams } from "ionic-angular";

/**
 * Generated class for the ShopCommentPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: "page-shop-comment",
  templateUrl: "shop-comment.html",
  providers: [StyleService]
})
export class ShopCommentPage extends BasePage {
  oboaut:boolean=false;
  notRemark: boolean;
  comment: any;

  /*用于保存需要显示的评论内容*/
  data = [];
  /*用于保存当前服务的所有评论内容*/
  allData: any;
mgs
  picrooturl = "";
  data1 = {
    service_id: ""
  };
  /** 购买服务数据 */
  goods = {};
  /** 评论数据 */
  service = {
    service_remark: []
  };
/**收藏 */
enshrine={
  collect_type:2,
  user_id:'',//用户id
  collected_id:''//收藏的服务ID
}
  /*用于保存当前服务的ID */
  service_id: any;
  is_collection;
  remark = [];
  remarks = [];
  ids: any;
data2={
  service_id:'',
  user_id:''
}
  private commenthasPic: number;
  private goodCommentCount: number;
  private normalCommentCount: number;
  private badCommentCount: number;

  /*被选中的 类别*/
  private isChose: any = 0;

  constructor(
    injector: Injector,
    public styleProvider: StyleService,
    public params: NavParams
  ) {
    super(injector);
    this.picrooturl = PIC_URL;
  }

  ionViewDidLoad() {
    console.log(this.params);
    this.service_id = this.params.data.service_id;
    this.is_collection=this.params.data.is_d;
    this.enshrine.user_id=this.params.data.user_id;
    this.enshrine.collected_id=this.params.data.goods_id
    this.data1.service_id = this.service_id;
    console.log(this.goods);
    console.log(this.service_id);

    /*得到当前服务的所有评论*/
    this.getCommentData();
    // this.getBuyServiceAPI();
    // this.getTheCount();
  }
  /** 切换 */
  // obout(stu) {
  //   this.oboaut = stu;
  //   console.log(this.oboaut)
  // }
  
  /**收藏 */
 async obout(){
    // this.enshrine.collect_type=0;
    this.data2.service_id=this.enshrine.collected_id;
    this.data2.user_id=this.enshrine.user_id
    let res = await this.styleProvider.collectionAPI(this.enshrine);
    let res2 = await this.styleProvider.getBuyServiceAPI(this.data2);
    console.log(res2.service.goods.is_collection);
    this.is_collection=res2.service.goods.is_collection;
    this.mgs=res.msg
    console.log(res);
    if (res.error_code == 0) {
      this.presentToast();
    } else {
      this.presentToast();
    }
  }
  /**取消收藏 */
//  async obout2(){
//     // this.enshrine.collect_type=1;
//     this.data2.service_id=this.enshrine.collected_id;
//     this.data2.user_id=this.enshrine.user_id
//     let res = await this.styleProvider.collectionAPI(this.enshrine);
//     let res2 = await this.styleProvider.getBuyServiceAPI(this.data2);
//     console.log(res2.service.goods);
//     console.log(res);
//     if (res.error_code == 0) {
      
//       console.log(this.enshrine)
//       this.mgs=res.msg;
//     // this.getBuyServiceAPI();
//       this.presentToast();
//     } else {
//       this.mgs=res.reason
//      console.log(res.msg) ;
//       this.presentToast();
//     }
//   }
  presentToast() {
    let toast = this.toastCtrl.create({
      message:  this.mgs,
      duration: 1500,
      position: 'middle',
      cssClass:'shoucang'
    });
    toast.onDidDismiss(() => {
      console.log('Dismissed toast');
    });

    toast.present();
  }
  /** 获取购买服务详情 */
  async getBuyServiceAPI() {
    let res = await this.styleProvider.getBuyServiceAPI(this.data1);
    console.log(res);
    console.log(this.data1)
    this.goods = res.service.goods;
    this.service = res.service;
    this.remark = this.service.service_remark;
    if (this.remark.length == 0) {
      this.notRemark = true;
    } else {
      this.notRemark = false;
    }
    console.log(this.goods['is_collection']);
    this.is_collection=this.goods['is_collection']
    console.log(this.remark);
    this.remarks = this.remark[0];
  }

  /*获取所有的评论详情*/
  async getCommentData() {
    let Param = {
      service_id: this.params.data.comment.service_id,
      remark_page: 1,
      remark_length: 10
    };
    let loader = this.loadingCtrl.create({
      content: ""
    });
    // console.log(this.params.data.comment.service_id)
    loader.present();
    let res = await this.styleProvider.getMoreRemarksAPI(Param);
    loader.dismiss();
    console.log(res);
    if (res.error_code == 0) {
      console.log(res.data);
      this.allData = res.data;
      this.data = this.allData;
      this.getTheCount();
    } else {
      console.log("网络错误");
      this.showAlert(res.reason);
    }
  }

  /*获取每个评论种类的个数*/
  getTheCount() {
    this.commenthasPic = this.data.filter(item => {
      return item.remark_img_first != "";
    }).length;
    this.goodCommentCount = this.allData.filter(item => {
      return item.remark_level == "5" || item.remark_level == "4";
    }).length;
    this.normalCommentCount = this.allData.filter(item => {
      return item.remark_level == "3" || item.remark_level == "2";
    }).length;
    this.badCommentCount = this.allData.filter(item => {
      return item.remark_level == "1";
    }).length;
  }

  changeStyle(item) {
    this.isChose = item;
    switch (this.isChose) {
      case 0:
        this.data = this.allData;
        break;
      case 1:
        this.data = this.allData.sort((a, b) => {
          return (
            new Date(b.remark_time).getTime() -
            new Date(a.remark_time).getTime()
          );
        });
        break;
      case 2:
        /*有图的*/
        this.data = this.allData.filter(item => {
          return item.remark_img_first != "";
        });
        this.commenthasPic = this.data.length;
        break;
      case 3:
        /*好评*/
        this.data = this.allData.filter(item => {
          return item.remark_level == "5" || item.remark_level == "4";
        });
        this.goodCommentCount = this.data.length;
        break;
      case 4:
        /*中评*/
        this.data = this.allData.filter(item => {
          return item.remark_level == "3" || item.remark_level == "2";
        });
        this.normalCommentCount = this.data.length;
        break;
      case 5:
        /*差评*/
        this.data = this.allData.filter(item => {
          return item.remark_level == "1";
        });
        this.badCommentCount = this.data.length;
        break;
      default:
        break;
    }
  }
Gopop(){
  this.navCtrl.pop()
}
  /**跳转到立即购买页面 */
  topromptlyBuy(price, name) {
    // this.navCtrl.push('PromptlyBuyPage');
    let data = this.params.data;
    let modal = this.modalCtrl.create("OrderPage", { service_price: this.params.data.service_price,
      nail_name: this.params.data.name,
      market_price:this.params.data.market_price,
      goods_id:this.params.data.goods_id,
      user_id:this.params.data.user_id,
      service_pic:this.params.data.service_pic  }); //PromptlyBuyPage  ShopInfoPage
    modal.present();
    console.log(this.params.data.service_price);
  }
}
